package cn.uniqueinfo.dp.controller;

import cn.uniqueinfo.dp.controller.base.BaseController;
import cn.uniqueinfo.dp.service.IFileService;
import cn.uniqueinfo.dp.util.ResultUtil;
import cn.uniqueinfo.dp.vo.FileVO;
import cn.uniqueinfo.dp.vo.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @author:朱元勋
 * @mail:395910063@qq.com
 * @date:2019-01-28 23:22:29
 * @description:文件资源
 */
@Slf4j
@Controller
@RequestMapping(value = "/file")
public class FileController extends BaseController<FileVO> {
    private IFileService service;

    public FileController(IFileService service) {
        this.service = service;
        this.baseService = service;
        this.ctlName = "file";
        this.tipName = "文件资源";
    }

    /** 支持多文件上传 */
    @PostMapping(value = "/upload")
    @ResponseBody
    public Result uploadFile(String attrs, HttpServletRequest request) {
        log.warn("文件对应的字段名称:" + attrs);
        List<MultipartFile> files = ((MultipartHttpServletRequest) request).getFiles("file");
        if (files != null && files.size() != 0) {
            for (MultipartFile mfile : files) {
                log.warn("name:" + mfile.getName());
                log.warn("ContentType:" + mfile.getContentType());
                log.warn("OriginalFilename:" + mfile.getOriginalFilename());
                log.warn("size:" + mfile.getSize());
                log.warn("isEmpty:" + mfile.isEmpty());
            }

        } else {
            log.error("没有上传的文件");
        }
        return ResultUtil.success();
    }
}
